package com.summli.basic.class02;

import java.util.Stack;

/**
 * 如何用栈结构实现队列结构
 */
public class Code06_TwoStacksImplementQueue {
    public static class TwoStacksQueue<T>{
        private Stack<T> stackPush;
        private Stack<T> stackPop;

        public TwoStacksQueue(){
            stackPop = new Stack<T>();
            stackPush = new Stack<T>();
        }
        public void add(T value){
            stackPush.push(value);
        }

        public T peek(){
            if(isEmpty()){
                return null;
            }
            if(stackPop.isEmpty()){
                while(!stackPush.empty()){
                    stackPop.push(stackPush.pop());
                }
            }
            return stackPop.peek();
        }

        public T poll(){
            if(isEmpty()){
                return null;
            }
            if(stackPop.isEmpty()){
                while(!stackPush.empty()){
                    stackPop.push(stackPush.pop());
                }
            }
            return stackPop.pop();
        }

        public Boolean isEmpty(){
            return stackPush.isEmpty() && stackPop.isEmpty();
        }
    }
    public static void main(String[] args) {
        TwoStacksQueue test = new TwoStacksQueue();
        test.add(1);
        test.add(2);
        test.add(3);
        System.out.println(test.peek());
        System.out.println(test.poll());
        System.out.println(test.peek());
        System.out.println(test.poll());
        System.out.println(test.peek());
        System.out.println(test.poll());
    }
}
